home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / circuits / irsim-ca.2 / irsim-ca / irsim-cap-9.2 / src / irsim / gentbl.c < prev    next >
C/C++ Source or Header  |  1994-10-17  |  6KB  |  193 lines

  1. /* 
  2.  *     ********************************************************************* 
  3.  *     * Copyright (C) 1988, 1990 Stanford University.                     * 
  4.  *     * Permission to use, copy, modify, and distribute this              * 
  5.  *     * software and its documentation for any purpose and without        * 
  6.  *     * fee is hereby granted, provided that the above copyright          * 
  7.  *     * notice appear in all copies.  Stanford University                 * 
  8.  *     * makes no representations about the suitability of this            * 
  9.  *     * software for any purpose.  It is provided "as is" without         * 
  10.  *     * express or implied warranty.  Export of this software outside     * 
  11.  *     * of the United States of America may require an export license.    * 
  12.  *     ********************************************************************* 
  13.  */
  14.  
  15. #include <stdio.h>
  16.  
  17. /* generate lookup tables for switch level simulation */
  18.  
  19. /* number of states desired for logic level */
  20. #define    NVALUES            4
  21.  
  22. /* total number of logic states we need */
  23. #define    NSPECTRUM        (2 * NVALUES + 1)
  24.  
  25. /* (NSPECTRUM choose 2) + NSPECTRUM */
  26. #define    NINTERVAL        ((NSPECTRUM *(NSPECTRUM - 1)) / 2 + NSPECTRUM)
  27.  
  28. char  *names[NSPECTRUM] = 
  29.   {
  30.     "DH", "WH", "CH", "cH", "Z", "cL", "CL", "WL", "DL"
  31.   };
  32.  
  33. FILE  *out;
  34.  
  35.  
  36. /* name of interval */
  37. char *pinterval( high, low )
  38.   {
  39.     static char  temp[100];
  40.  
  41.     if( high != low )
  42.     sprintf( temp, "%s%s", names[high], names[low] );
  43.     else
  44.     sprintf( temp, "%s", names[high] );
  45.     return( temp );
  46.   }
  47.  
  48.  
  49. /* return strength of value */
  50. int strength( i )
  51.   register int  i;
  52.   {
  53.     if( (i -= NVALUES) < 0 )
  54.     i = -i;
  55.     return( i );
  56.   }
  57.  
  58. #define min( A, B )            ( ((A) < (B)) ? (A) : (B) )
  59. #define max( A, B )            ( ((A) > (B)) ? (A) : (B) )
  60.  
  61. /* find the enclosing interval */
  62. char *span( ihigh, ilow, jhigh, jlow )
  63.   {
  64.     return( pinterval( min( ihigh, jhigh ), max( ilow, jlow ) ) );
  65.   }
  66.  
  67.  
  68. /* merge two intervals using least-upper bound operation */
  69. char *merge( ihigh, ilow, jhigh, jlow )
  70.   {
  71.     register int  ahigh, alow;
  72.  
  73.     if( strength( ihigh ) > strength( jhigh ) )
  74.     ahigh = ihigh;
  75.     else if( strength( ihigh ) < strength( jhigh ) )
  76.     ahigh = jhigh;
  77.     else if( ihigh < jhigh )
  78.     ahigh = ihigh;
  79.     else
  80.     ahigh = jhigh;
  81.  
  82.     if( strength( ilow ) > strength( jlow ) )
  83.     alow = ilow;
  84.     else if( strength( ilow ) < strength( jlow ) )
  85.     alow = jlow;
  86.     else if( ilow > jlow )
  87.     alow = ilow;
  88.     else
  89.     alow = jlow;
  90.  
  91.     return( pinterval( ahigh, alow ) );
  92.   }
  93.  
  94.  
  95. /* convert interval to use weak values */
  96. char *weak( i, j )
  97.   {
  98.     if( i == 0 )
  99.     i = 1;
  100.     else if( i == (NSPECTRUM - 1) )
  101.     i = NSPECTRUM - 2;
  102.     if( j == 0 )
  103.     j = 1;
  104.     else if( j == (NSPECTRUM - 1) )
  105.     j = NSPECTRUM - 2;
  106.     return( pinterval( i, j ) );
  107.   }
  108.  
  109.  
  110. int main()
  111.   {
  112.     register int  i, j, k, ii, jj, interval2;
  113.  
  114.     if( (out = fopen( "stables.c", "w" )) == NULL )
  115.       {
  116.     fprintf( stderr, "cannot open stables.c for output\n" );
  117.     exit( 1 );
  118.       }
  119.  
  120.     fprintf( out, "/* DO NOT EDIT: THIS FILE IS GENERATED USING gentbl */\n");
  121.     fprintf( out, "/* names for each value interval */\n" );
  122.     fprintf( out, "char *node_values[%d] = {\n", NINTERVAL+1 );
  123.     fprintf( out, "\t\"EMPTY\",\n" );
  124.     for( i = 0; i < NSPECTRUM; i += 1 )
  125.     for( j = i; j < NSPECTRUM; j += 1 )
  126.         fprintf( out, "\t\"%s\",\n", pinterval( i, j ) );
  127.     fprintf( out, "};\n\n" );
  128.  
  129.     fprintf( out, "/* index for each value interval */\n" );
  130.     fprintf( out, "#define EMPTY\t%d\n\n", 0 );
  131.     for( i = 0, k = 1; i < NSPECTRUM; i += 1 )
  132.     for( j = i; j < NSPECTRUM; j += 1, k += 1 )
  133.         fprintf( out, "#define %s\t%d\n", pinterval( i, j ), k );
  134.     fprintf( out, "\n" );
  135.  
  136.     fprintf( out, "/* conversion between interval and logic value */\n" );
  137.     fprintf( out, "char logic_state[%d] = {\n", NINTERVAL+1 );
  138.     fprintf( out, "  0,\t/* EMPTY */\n" );
  139.     for( i = 0; i < NSPECTRUM; i += 1 )
  140.     for( j = i; j < NSPECTRUM; j += 1 )
  141.         if( i < NVALUES && j < NVALUES )
  142.         fprintf( out, "  HIGH,\t/* %s */\n", pinterval( i, j ) );
  143.         else if( i > NVALUES && j > NVALUES )
  144.         fprintf( out, "  LOW,\t/* %s */\n", pinterval( i, j ) );
  145.         else
  146.         fprintf( out, "  X,\t/* %s */\n", pinterval( i, j ) );
  147.     fprintf( out, "};\n\n" );
  148.  
  149.     fprintf( out, "/* transmit interval through switch */\n" );
  150.     fprintf( out, "char transmit[%d][4] = {\n", NINTERVAL+1 );
  151.     fprintf( out, "  0,\t0,\t0,\t0,\t/* EMPTY */\n" );
  152.     for( i = 0; i < NSPECTRUM; i += 1 )
  153.     for( j = i; j < NSPECTRUM; j += 1 )
  154.       {
  155.         fprintf( out, "  Z," );              /* off switch */
  156.         fprintf( out, "\t%s,", pinterval( i, j ) );      /* on switch */
  157.         fprintf( out, "\t%s,", span( i, j, NVALUES, NVALUES ) );
  158.                               /* unknown switch */
  159.         fprintf( out, "\t%s,", weak( i, j ) );      /* weak switch */
  160.         fprintf( out, "\t/* %s */\n", pinterval( i, j ) );
  161.       }
  162.     fprintf( out, "};\n\n" );
  163.  
  164.     /* compute smallest power of two greater than NINTERVAL */
  165.     for( interval2 = 1; interval2 < NINTERVAL; interval2 <<= 1 );
  166.  
  167.     fprintf( out, "/* result of shorting two intervals */\n" );
  168.     fprintf( out, "char smerge[%d][%d] = {\n", NINTERVAL+1, NINTERVAL+1 );
  169.     fprintf( out, "\n/* EMPTY */\n" );
  170.     for( i = 0; i <= NINTERVAL; i +=1 )
  171.       {
  172.     fprintf( out, "  0   ," );            /* EMPTY */
  173.     if( i % 8 == 7 )
  174.         fprintf( out, "\n" );
  175.       }
  176.  
  177.     for( i = 0; i < NSPECTRUM; i += 1 )
  178.     for( j = i; j < NSPECTRUM; j += 1 )
  179.       {
  180.         fprintf( out, "\n/* %s */\n", pinterval( i, j ) );
  181.         fprintf( out, "  0   ," );
  182.         for( ii = 0, k = 1; ii < NSPECTRUM; ii += 1 )
  183.         for( jj = ii; jj < NSPECTRUM; jj += 1, k += 1 )
  184.           {
  185.             fprintf( out, "  %-4s,", merge( i, j, ii, jj ) );
  186.             if( k % 8 == 7 )
  187.             fprintf( out, "\n" );
  188.           }
  189.       }
  190.     fprintf( out, "\n};\n" );
  191.     return 0;
  192.   }
  193.